//
//  SMHSettingView.swift
//  SMHCommerce
//
//  Created by lvzhao on 2020/6/5.
//  Copyright © 2020 lvzhao. All rights reserved.
//

import UIKit
import ReactiveCocoa


class SMHSettingView: LZBaseView ,UITableViewDelegate,UITableViewDataSource{

    var viewModel = SMHSettingViewModel()

    override init(viewModel: LZBaseViewModel) {
        super.init(viewModel: viewModel)
        self.viewModel = viewModel as! SMHSettingViewModel
      
        self.setupUI()
        
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.tableView.reloadData()
        }
        
    }

    required init?(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        
        let headView = UIView()
        headView.backgroundColor = UIColorFromHex(rgbValue: 0xF2F2F2)
        headView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.5)
        self.tableView.tableHeaderView = headView
        
        
        let footView = UIView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height:65))
        footView.backgroundColor = UIColorFromHex(rgbValue: 0xF2F2F2)
        
        let logoutBtn = UIButton.init(type: .custom)
        logoutBtn.backgroundColor = .white
        logoutBtn.setTitle("退出登录", for: .normal)
        logoutBtn.setTitleColor(UIColorFromHex(rgbValue: 0xFF661B), for: .normal)
        logoutBtn.titleLabel?.font = ktextFont(size: 16)
        logoutBtn.frame = CGRect(x: 0, y: 10, width: kScreenWidth, height: 55)
        footView.addSubview(logoutBtn)
        logoutBtn.reactive.controlEvents(.touchUpInside).observeResult { (button) in
            print("哈哈")
            print("退出登录");
           let window = UIApplication.shared.keyWindow
           
           let loginVC = SMHLoginViewController.init()
           let loginNav = LZBaseNavViewController.init(rootViewController: loginVC)
           window?.rootViewController = loginNav
        }
        self.tableView.tableFooterView = footView

        
//        logoutBtn.rx.tap.subscribe { (onNext) in
//
//
//        }.dispose()
        
    }
          
    //MARK:UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataArray.count
        
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let settingCell = tableView.dequeueReusableCell(withIdentifier: "SMHSettingCell", for: indexPath) as! SMHSettingCell
        settingCell.modelObject = self.viewModel.dataArray[indexPath.row]
        return settingCell
    }
    
    //MARK:UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let settinggModel = self.viewModel.dataArray[indexPath.row] as! SMHSettingModel
        
        switch settinggModel.title {
        case "修改昵称":
            let nickNameVC = SMHModifyNickNameViewController()
            currentVC()?.navigationController?.pushViewController(nickNameVC, animated: true)
        case "关于我们":

            let webVC = LZWebViewController()
            webVC.webUrl = "http://m.sumaihuo.shop/aboutUs.html"
            currentVC()?.navigationController?.pushViewController(webVC, animated: true)
        case "清除缓存":
            self.viewModel.clearCache {}
            
            
        case "修改登录密码":
            let modifyLoginPwdVC = SMHModifyLoginPwdViewController()
            currentVC()?.navigationController?.pushViewController(modifyLoginPwdVC, animated: true)
            
        default:
            break
        }
        
        
    }
    
    lazy var tableView : LZBaseTableView = {
        let tableView = LZBaseTableView.init(frame: CGRect.zero, style: .plain)
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = 55
        tableView.backgroundColor = UIColorFromHex(rgbValue: 0xF2F2F2)
        tableView.register(SMHSettingCell.self, forCellReuseIdentifier: "SMHSettingCell")
        return tableView
    }()
    
}
